
$(function(){
    host="http://10.128.33.182:8000/admin/";
    tb1_1={
        editRow:undefined,
        add:function(){
          console.log("save1_1");
            $('#save1_1,#redo1_1').show();
            if(this.editRow==undefined){
                $('#dg1_1').datagrid('insertRow',{
                    index:0,
                    row:{

                    }
                });
                $('#dg1_1').datagrid('beginEdit',0);
                this.editRow=0;
                $('#dg1_1').datagrid('hideColumn','role');
                $('#dg1_1').datagrid('removeEditor','role');
             }

        },
        remove:function(){
            var rows=$('#dg1_1').datagrid('getSelections');
            console.log(rows[0]);
            if(rows.length>0){
                $.messager.confirm('确认信息','确定要删除记录?',function(r){
                    if (r){
                       $.ajax({
                           url: host+'account/remove',
                           type: 'POST',
                           dataType: 'json',
                           data: {workNum:rows[0].workNum},
                       })
                       .done(function(data) {
                            console.log(data);
                            if(data.status==200){
                                 console.log("success");
                                 $('#dg1_1').datagrid('loaded');
                                 $('#dg1_1').datagrid('load');
                                 $('#dg1_1').datagrid('unselectAll');
                                 $.messager.show({
                                    title:'提示',
                                    msg:"您已成功删除",
                                 })
                             }else{
                                console.log('删除失败');
                             }
                          
                       })
                       .fail(function() {
                           console.log("error");
                       })
                       .always(function() {
                           console.log("complete");
                       });
                       
                     }
            });
            }else{
                 $.messager.alert("提示","请至少选择一行要删除的记录",'info');
            }
        },
        edit:function(){
            $("#dg1_1").datagrid('addEditor', {    
                        field : 'role',    
                         editor: {  
                        type: 'combobox',  
                        options: {  
                            data: [{
                                text:'普通雇员',
                                value:'1',
                            },
                            {
                                text:'管理员',
                                value:'0',
                            }
                            ], 
                            valueField: 'value',  
                            textField: 'text',  
                             editable:false,
                            panelHeight: 'auto'  
                        }  
                    },  
                    });  
            var rows=$('#dg1_1').datagrid('getSelections');
            //问：rows是按选中的次序来排？
            if(rows.length==1){
                 if(tb1_1.editRow!=undefined){
                    console.log("此时已有其他行处于编辑状态");
                    $('#dg1_1').datagrid('endEdit',tb1_1.editRow);
                }
                if(tb1_1.editRow==undefined){
                   console.log("此时没有其他行处于编辑状态");
                   var rowIndex=$('#dg1_1').datagrid('getRowIndex',rows[0]);
                   $('#save1_1,#redo1_1').show();
                    $('#dg1_1').datagrid('beginEdit',rowIndex);
                    tb1_1.editRow=rowIndex;
                    //上一个点击了编辑的行，即使执行了endedit,但默仍为选中状态，
                    $('#dg1_1').datagrid('unselectRow',rowIndex);
                }
            }else{
                $.messager.alert("警告","修改只能选择一行",'warning');
            }
        },
        save:function(){
           // $('#save1_2,#hide').hide();
           // this.editRow=false;
            $('#dg1_1').datagrid('endEdit',this.editRow);
          
        },
        redo:function(){
            $('#save1_1,#redo1_1').hide();
            this.editRow=undefined;
             $('#dg1_1').datagrid('rejectChanges');
              $("#dg1_1").datagrid("showColumn", "role");
        }
    };
   
//});
    $('#dg1_1').datagrid({
        url:host+'account/list', 
        width:800,
        queryParams:{
            start:0,
            length:5
        },
        title:'账户列表',
        fitColumns:true,
        columns:[[

            {   field:'workNum',
                title:'工号',
                width:100,
                editor:{
                    type:'validatebox',
                    options:{
                        required:true,
                    }
                }
            },
            {
                field:'role',
                title:'角色',
                width:100,
                editor: {  
                    type: 'combobox',  
                    options: {  
                        data: [{
                            text:'普通雇员',
                            value:'1',
                        },
                        {
                            text:'管理员',
                            value:'0',
                        }
                        ], 
                        valueField: 'value',  
                        textField: 'text',  
                         editable:false,
                        panelHeight: 'auto'  
                    }  
                },  
           },
           {
                field:'password',//field字段必须，为空也得写上，否则宽度为5px
                title:'密码',
                width:100,
                editor:{
                    type:'validatebox',
                    options:{
                        required:true,
                    }
                }
           }
        ]],
       
        pagination:true,
        pageSize:5,
        pageList:[5,10,15],
        pageNumber:1,
        toolbar:'#tb1_1',
        onSelectPage:function(pageNumber,pageSize){
            console.log(pageNumber+":"+pageSize);
        },
        //在endEdit后自动触发检验是否符合validate才会执行
        onAfterEdit:function(rowIndex,rowData,changes){
                console.log("执行了onAfterEdit");
                //console.log(rowData);
                //console.log(changes);
                $('#save1_1,#redo1_1').hide();
                var inserted=$('#dg1_1').datagrid('getChanges','inserted');
                var updated=$('#dg1_1').datagrid('getChanges','updated');
                //新增用户
                if(inserted.length>0){
                         $.ajax({
                           url: host+'account/create',
                           type: 'POST',
                           dataType: 'json',
                           data: {
                                    workNum: inserted[0].workNum,
                                    password: hex_sha1( inserted[0].workNum+inserted[0].password),
                                },
                       })
                       .done(function(data) {
                            console.log(data);
                            if(data.status==200){
                                 console.log("success");
                                 $('#dg1_1').datagrid('loaded');
                                 $('#dg1_1').datagrid('load');
                                
                                 $.messager.show({
                                    title:'提示',
                                    msg:"您已成功添加",
                                 });
                                  tb1_1.editRow=undefined;
                             }else if(data.status==303){
                              $('#dg1_1').datagrid('reload');
                               $.messager.alert("警告，添加失败！","该用户名已存在",'warning');
                             }
                          
                       })
                       .fail(function() {
                        $('#dg1_1').datagrid('reload');
                            $.messager.alert("警告，添加失败！","不明错误",'warning');
                       })
                       .always(function() {
                           console.log("complete");
                       });

                }
                 //修改用户
                if(updated.length>0){
                
                        $.ajax({
                           url: host+'account/set',
                           type: 'POST',
                           dataType: 'json',
                           data: {
                                    workNum: updated[0].workNum,
                                    password: hex_sha1( updated[0].workNum+updated[0].password),
                                    role:updated[0].role,
                                },
                       })
                       .done(function(data) {
                            console.log(data);
                            if(data.status==200){
                                 console.log("success");
                                 $('#dg1_1').datagrid('loaded');
                                 $('#dg1_1').datagrid('load');
                                 
                                  $('#dg1_1').datagrid('reload');
                                 $.messager.show({
                                    title:'提示',
                                    msg:"您已成功修改",
                                 });
                                  tb1_1.editRow=undefined;
                             }else if(data.status==304){
                               $.messager.alert("警告，修改失败！","该用户不存在",'warning');
                             }
                          
                       })
                       .fail(function() {
                           $.messager.alert("警告，修改失败！","不明错误原因",'warning');
                       })
                       .always(function() {
                           
                       });//end ajax
                }//end  if(updated.length>0)
                console.log("1111");
                 $('#dg1_1').datagrid('unselectAll');
                 $("#dg1_1").datagrid("showColumn", "role");
                 tb1_1.editRow=undefined;
               
        },//end  onAfterEdit:
        onDblClickRow:function(rowIndex,rowData){
            console.log("执行了双击操作");
            //console.log(rowData);
            if(tb1_1.editRow!=undefined){
                console.log("此时已有其他行处于编辑状态");
                $('#dg1_1').datagrid('endEdit',tb1_1.editRow);

            }
            if(tb1_1.editRow==undefined){
               console.log("此时没有其他行处于编辑状态");
               $('#save1_1,#redo1_1').show();
                $('#dg1_1').datagrid('beginEdit',rowIndex);
                tb1_1.editRow=rowIndex;
            }
        }//end ondblclickrow
    });
    
});
